home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 262 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: qsort help
  5. Date: Wed, 03 Jan 96 20:33:05 GMT
  6. Organization: none
  7. Message-ID: <820701185snz@genesis.demon.co.uk>
  8. References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu> <4cdomq$jgt@dub-news-svc-5.compuserve.com> <4ce4oi$hsd@news.iag.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ce4oi$hsd@news.iag.net> jatmon@iag.net "John R Buchan" writes:
  15.  
  16. >If this function is to be passed to qsort, it needs to be something like:
  17. >
  18. >int compar( const void *a, const void *b)
  19. >{
  20. >  const int *c = a, *d = b;
  21. >  return *c - *d;
  22. >}
  23.  
  24. That's a trick that only works when you know something about the ranges
  25. of the elements involved, for instance an implementation might use it
  26. in the strcmp() library function when it knows that (sizeof(int) > 1),
  27. or more precisely that int and char have suitable ranges. In the general
  28. case *c - *d can overflow resulting in undefined behaviour.
  29.  
  30. -- 
  31. -----------------------------------------
  32. Lawrence Kirby | fred@genesis.demon.co.uk
  33. Wilts, England | 70734.126@compuserve.com
  34. -----------------------------------------
  35.